package com.rc.saas.tenant.service.statistics;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.common.utils.StringUtil;
import com.rc.saas.tenant.mapper.member.MemberInfoMapper;
import com.rc.saas.tenant.model.channel.RcChannelApply;
import com.rc.saas.tenant.model.channel.RcChannelApplyExample;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.member.RcMemberFlag;
import com.rc.saas.tenant.model.operator.RcOperator;
import com.rc.saas.tenant.model.order.OrderInfo;
import com.rc.saas.tenant.model.recharge.RechargeOrder;
import com.rc.saas.tenant.search.statistics.ChargeStatisticsSearch;
import com.rc.saas.tenant.service.channel.RcChannelApplyService;
import com.rc.saas.tenant.service.member.RcMemberFlagService;
import com.rc.saas.tenant.service.operator.RcOperatorService;
import com.rc.saas.tenant.service.order.OrderInfoService;
import com.rc.saas.tenant.service.order.OrderRefundApplyService;
import com.rc.saas.tenant.service.recharge.RechargeOrderService;
import com.rc.saas.tenant.vo.order.ChargeStatisticsResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.ListResourceBundle;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Service
public class StatisticsServiceImpl implements StatisticsService {


    @Autowired
    private RcChannelApplyService channelService;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private RechargeOrderService rechargeOrderService;

    @Autowired
    private OrderRefundApplyService orderRefundApplyService;

    @Autowired
    private RcMemberFlagService memberFlagService;

    @Autowired
    private MemberInfoMapper memberInfoMapper;

    @Autowired
    private RcOperatorService operatorService;

    @Override
    public PageInfo listPageChargeStatistics(ChargeStatisticsSearch search) {
        if (!StringUtils.isEmpty(search.getOperator())) {
            RcOperator operator = operatorService.selectOperatorApplyByOperator(search.getOperator());
            if (operator != null && operator.getMemberId() != 0) {
                search.setMemberId(operator.getMemberId());
            } else {
                return new PageInfo(new ArrayList());
            }
        }

        //根据订单号更新用户昵称
        if (StringUtils.isBlank(search.getNickname()) && StringUtils.isNotBlank(search.getOutOrderNum())) {
            OrderInfo orderInfo = orderInfoService.selectOrderByOrderNum(search.getOutOrderNum());
            RechargeOrder rechargeOrder = rechargeOrderService.selectByRechargeOrderNum(search.getOutOrderNum());
            if (orderInfo != null) {
                MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(orderInfo.getMemberId());
                search.setNickname(memberInfo.getNickname());
            } else if (rechargeOrder != null) {
                MemberInfo memberInfo = memberInfoMapper.selectByPrimaryKey(rechargeOrder.getMemberId());
                search.setNickname(memberInfo.getNickname());
            }
        }

        PageHelper.startPage(search.getPageNo(), search.getPageSize());
        List<ChargeStatisticsResult> list = orderInfoService.listChargeStatistics(search);
        ConcurrentHashMap<Long, ChargeStatisticsResult> map = new ConcurrentHashMap<>();
        for (ChargeStatisticsResult result : list) {
            map.put(result.getMemberId(), result);
        }

        List<Long> memberIdList = list.stream().map(ChargeStatisticsResult::getMemberId).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(memberIdList)) {
            List<ChargeStatisticsResult> rechargeList = rechargeOrderService.listRechargeStatistics(memberIdList);
            for (ChargeStatisticsResult recharge : rechargeList) {
                ChargeStatisticsResult result = map.get(recharge.getMemberId());
                result.setRechargeCount(recharge.getRechargeCount());
            }

            List<ChargeStatisticsResult> refundList = orderRefundApplyService.listRefundStatistics(memberIdList);
            for (ChargeStatisticsResult refund : refundList) {
                ChargeStatisticsResult result = map.get(refund.getMemberId());
                result.setRefundCount(refund.getRefundCount());
            }

            List<RcMemberFlag> memberFlagList = memberFlagService.listMemberFlag(memberIdList);
            for (RcMemberFlag memberFlag : memberFlagList) {
                StringBuffer memberType = new StringBuffer();
                //String memberType = "";
                if ("1".equals(memberFlag.getIsOperator())) {
                    memberType.append("3");
                    //memberType = "3";

                }
                if ("1".equals(memberFlag.getIsChannelLevel())) {
                    memberType.append("2");
                    //memberType = "2";

                }
                if ("1".equals(memberFlag.getIsEnterpriseLevel())) {
                    memberType.append("1");
                    //memberType = "1";
                }
                if (StringUtil.isBlank(memberType.toString().trim())) {
                    memberType.append("0");
                }
                ChargeStatisticsResult result = map.get(memberFlag.getMemberId());
                result.setMemberType(memberType.toString());
            }

        }
        return new PageInfo(list);
    }
}
